The events of the last few years revealed an acute need for tools to systematically model and analyze large financial networks. Many applications of such tools include the forecasting of systemic failures and analyzing probable effects of economic policy decisions. We consider optimizing the amount and structure of a bailout in a borrower-lender network: Given a fixed amount of cash to be injected into the system, how should it be distributed among the nodes in order to achieve the smallest overall amount of unpaid liabilities or the smallest number of nodes in default? We develop an exact algorithm for the problem of minimizing the amount of unpaid liabilities, by showing that it is equivalent to a linear program. For the problem of minimizing the number of defaults, we develop an approximate algorithm using a reweighted l1 minimization approach. We illustrate this algorithm using an example with synthetic data for which the optimal solution can be calculated exactly, and show through numerical simulation that the solutions calculated by our algorithm are close to optimal.